//
//  GalleryViewController.swift
//  jiahui
//
//  Created by 3tee on 2024/11/27.
//

import UIKit
import avd_sdk
import SnapKit
import RxSwift

class GalleryViewController: UIViewController {

    
    lazy var bag = DisposeBag()
    let padding = 8.0
    lazy var renderHeiget2: CGFloat = {
        let height = (screen_height - 66 - navigationBar_height - self.padding * 5) / 2
        return height
    }()
    //let renderHeiget = (screen_height - 66 - navigationBar_height - self.padding * 3) / 2
    
    var mRenders = [AVDVideoView]()
    
    lazy var render1: AVDVideoView = {
        let render = AVDVideoView()
        render.layer.borderWidth = 0.5
        render.layer.borderColor = UIColor.white.cgColor
        render.layer.cornerRadius = 8
        render.layer.masksToBounds = true
        return render
    }()
    
    lazy var render2: AVDVideoView = {
        let render = AVDVideoView()
        render.layer.borderWidth = 0.5
        render.layer.borderColor = UIColor.white.cgColor
        render.layer.cornerRadius = 8
        render.layer.masksToBounds = true
        return render
    }()
    
    lazy var render3: AVDVideoView = {
        let render = AVDVideoView()
        render.layer.borderWidth = 0.5
        render.layer.borderColor = UIColor.white.cgColor
        render.layer.cornerRadius = 8
        render.layer.masksToBounds = true
        return render
    }()
    
    lazy var render4: AVDVideoView = {
        let render = AVDVideoView()
        render.layer.borderWidth = 0.5
        render.layer.borderColor = UIColor.white.cgColor
        render.layer.cornerRadius = 8
        render.layer.masksToBounds = true
        return render
    }()
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("获取当前已经发布的摄像头列表 \(ThreeTManager.shardInstance.cameraPubList())")
        displayCameras()
    }
    
   
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(render1)
        render1.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.top.equalTo(8)
            make.width.equalTo((screen_width - padding * 3) / 2)
            make.height.equalTo(renderHeiget2)
        }
        
        view.addSubview(render2)
        render2.snp.makeConstraints { make in
            make.left.equalTo(render1.snp.right).offset(8)
            make.top.equalTo(8)
            make.width.equalTo(render1.snp.width)
            make.height.equalTo(render1.snp.height)
        }
        
        view.addSubview(render3)
        render3.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.top.equalTo(render1.snp.bottom).offset(4)
            make.width.equalTo(render1.snp.width)
            make.height.equalTo(render1.snp.height)
        }
        
        view.addSubview(render4)
        render4.snp.makeConstraints { make in
            make.left.equalTo(render1.snp.right).offset(8)
            make.top.equalTo(render2.snp.bottom).offset(4)
            make.width.equalTo(render1.snp.width)
            make.height.equalTo(render1.snp.height)
        }
        view.subviews.forEach { view in
            if view.isKind(of: AVDVideoView.self) {
                let render = view as! AVDVideoView
                mRenders.append(render)
            }
        }
        
            ThreeTManager.shardInstance.cameraOpenNoti.subscribe {[weak self] deviceid in
                print("画廊模式监听到摄像头打开 id = \(deviceid)")
                guard let strongself = self else { return }
                
                let freeRender = strongself.getFreeRender()
                freeRender.setVideoId(deviceid)
                ThreeTManager.shardInstance.attachRender(freeRender)
                
                
            }.disposed(by: bag)
            
            ThreeTManager.shardInstance.cameraCloseNoti.subscribe {[weak self] deviceid in
                guard let strongself = self else { return }
                print("画廊模式监听到摄像头关闭 id = \(deviceid)")
                let render = strongself.getRender(did: deviceid)
                ThreeTManager.shardInstance.detachRender(render)
                render.setVideoId("")
            }.disposed(by: bag)
      
            
    }
    
    func getFreeRender() -> AVDVideoView {
        var tempRender = AVDVideoView()
        for idx in 0..<mRenders.count {
            tempRender = mRenders[idx]
            if tempRender.getVideoId() == "" {
                return tempRender
            }
        }
        return tempRender
    }
    
    func getRender(did: String) -> AVDVideoView {
        var tempRender = AVDVideoView()
        for idx in 0..<mRenders.count {
            tempRender = mRenders[idx]
            if tempRender.getVideoId() == did{
                return tempRender
            }
        }
        return tempRender
    }
    
    func displayCameras() {
        ThreeTManager.shardInstance.cameraPubList().forEach { deviceid in
            print("当前渲染的摄像头id = \(deviceid)")
            let idx = ThreeTManager.shardInstance.cameraPubList().firstIndex(of: deviceid) ?? 0
            let render = mRenders[idx]
            render.setVideoId(deviceid)
            ThreeTManager.shardInstance.attachRender(render)
        }
    }
    
   
    
   
}
extension GalleryViewController{
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        print("画廊模式已经disappear")
        cleanRenders()
    }
    
    func cleanRenders(){
        mRenders.forEach {[weak self] render in
           //guard let strongself = self else{return}
            ThreeTManager.shardInstance.detachRender(render)
        }
    }
}
